package com.minhua.jijiehao2.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author moo
 * @date 2017/11/7.
 * @describe MVP
 */

public abstract class BaseFragment<V, P extends BaseMvpPresenter<V>> extends Fragment {

    protected P mPresenter;
    protected View mView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getPresenter();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter.attach((V) this);
        initView();
        initData();
    }

    /**
     * 初始化数据（需要时重写）
     */
    protected void initData() {

    }

    /**
     * 初始化布局
     */
    protected abstract void initView();


    /**
     * 获取布局资源id
     *
     * @return 布局资源id
     */
    protected abstract int getLayoutId();

    /**
     * 获取presenter
     *
     * @return presenter
     */
    protected abstract P getPresenter();
}
